// 节点的定义
function Node(data, left, right) {
  this.data = data;
  this.left = left;
  this.right = right;
  this.show = show;
}

function show() {
  return data;
}
// 二叉查找树的类
function BST() {
  this.root = null;
  this.insert = insert;
  this.preOrder = preOrder;
  this.inOrder = inOrder;
  this.postOrder = postOrder;
  this.find = find;
  this.getMin = getMin;
  this.getMax = getMax;
  this.remove = remove;
}
// 插入
function insert(data) {
  let n = new Node(data, null, null);
  if (this.root == null) {
      this.root = n;
  } else {
      let current = this.root;
      let parent;
      while(true) {
          parent = current;
          if (data < current.data) {
              current = current.left;
              if (current == null) {
                  parent.left = n;
                  break;
              }
          } else {
              current = current.right;
              if (current == null) {
                  parent.right = n;
                  break;
              }
          }
      }
  }
}
// 中序
function inOrder(node) {
  if (!(node ==null)) {
      inOrder(node.left);
      console.log(node.show() + '');
      inOrder(node.right);
  }
}
// 先序
function preOrder( node ) {
  if( !(node == null )){
      console.log( node.show() + ' ');
      preOrder( node.left );
      preOrder( node.right );
  }
}
// 后续
function postOrder ( node ) {
  if( !(node == null ) ){
      postOrder( node.left );
      postOrder( node.right );
      console.log( node.show() + ' ');
  }
}
// 查找最小值
function getMin( ) {
  var current = this.root;
  while ( !( current.left == null ) ){
      current = current.left;
  }
  return current.show();
}
// 查找最大值
function getMax () {
  var current = this.root;
  while ( !( current.right == null ) ) {
      current = current.right;
  }
  return current.show();
}
// 查找给定值
function find ( data ) {
  var current = this.root;
  while ( current != null ){
      if( current.data == data ){
          return current;
      }else if( current.data < data ){
          current = current.right;
      }else{
          current = current.left;
      }
  }
  return null;
}
// 删除操作
function remove( data ) {
  removeNode( this.root , data);
}
// 查找最小值
function getSmallest(node) {
  if (node.left == null) {
      return node;
  }
  else {
      return getSmallest(node.left);
  }
}
//删除节点
function removeNode( node , data ) {
  if( node == null ) {
      return null;
  }
  if(data == node.data) {
      // 没有子节点的节点
      if(node.left == null && node.right == null) {
          return null;
      }
      // 没有左子节点的节点
      if(node.left == null) {
          return node.right;
      }
      // 没有右子节点的节点
      if(node.right == null) {
          return node.left;
      }
      // 有2个子节点的节点
      var tempNode = getSmallest(node.right);
      node.data = tempNode.data;
      node.right = removeNode(node.right,tempNode.data);
      return node;

  }else if(data < node.data) {
      node.left = removeNode( node.left,data);
      return node;
  }else {
      node.right = removeNode( node.right,data);
      return node;
  }
}
let bstTree = new BST()

bstTree.insert(10)
bstTree.insert(12)
bstTree.insert(18)
bstTree.insert(9)
console.log(bstTree.root, 'bstTree');
